package com.example.moveclock.activities;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.widget.AnalogClock;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.example.moveclock.common.TimeHandler;
import com.example.moveclock.views.AngryFaggotsView;
import com.example.moveclock.views.CircuitOfJoystickView;
import com.example.moveclock.views.JoystickView;

public class AngryFaggotsGameActivity extends Activity {
	private RelativeLayout rlayout;
	private CircuitOfJoystickView circuit;
	private JoystickView joystick;

	private TimeHandler mHandler;
	private Timer timer = new Timer();
	private Drawable angryFaggot;
	private Resources res;
	private AnalogClock clock;

	/** A handle to the thread that's actually running the animation. */
	private AngryFaggotsView.FaggotsThread mFaggotsThread;

	/** A handle to the View in which the game is running. */
	private AngryFaggotsView mFaggotsView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// tell system to use the layout defined in our XML file
		setContentView(R.layout.activity_angry_faggots_game);

		// get handles to the LunarView from XML, and its LunarThread
		mFaggotsView = (AngryFaggotsView) findViewById(R.id.faggots);
		mFaggotsThread = mFaggotsView.getThread();
		
		mFaggotsThread.doStart();

		// we were just launched: set up a new game
		// setContentView(R.layout.activity_angry_faggots_game);
		// rlayout = (RelativeLayout) findViewById(R.id.tLayout);
		// addJoystickOnView();
		// res = getResources();
		//
		// mHandler = new TimeHandler();
		// clock = (AnalogClock) findViewById(R.id.analogClock2);
		//
		// mHandler.setClock(clock);
		// mHandler.setJoystick(joystick);
		// mHandler.setRlayout(rlayout);
		// startTimer();
		// createFaggots();
	}

	private void createFaggots() {
		angryFaggot = res.getDrawable(R.drawable.face);
		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				angryFaggot.setBounds(5, 5, 15, 15);
			}
		}, 0, 1000);
	}

	private void startTimer() {
		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				mHandler.sendMessage(new Message());
			}
		}, 0, 10);
	}

	private void addJoystickOnView() {
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.addRule((int) RelativeLayout.ALIGN_PARENT_BOTTOM);
		params.addRule((int) RelativeLayout.ALIGN_PARENT_RIGHT);
		params.setMargins(0, 0, 20, 20);

		circuit = new CircuitOfJoystickView(this);
		circuit.setLayoutParams(params);
		rlayout.addView(circuit);

		joystick = new JoystickView(this);
		joystick.setLayoutParams(params);
		rlayout.addView(joystick);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_joystick_control_game, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		timer.cancel();
		super.onDestroy();
	}

}
